package com.toocms.jinjuhang.dialog.options

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.databinding.DialogOptionsBinding

class OptionsDialog : DialogFragment() {
    var binding: DialogOptionsBinding? = null
    var viewModel: OptionsModel? = null
    var title: String? = null
        set(value) {
            field = value
            viewModel?.titleObservableField?.set(value)
        }
    var optionsItems: List<out OptionsItem>? = null
        set(value) {
            field = value
            viewModel?.optionsItems = value
        }
    var isShowConfirm:Boolean=false
    set(value) {
        field = value
        viewModel?.isShowConfirmObservableBoolean?.set(value)
    }
    var onOptionClickListener: ((option: OptionsItem, tag: String?) -> Unit)? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.dialog_options, container, false)
        viewModel = OptionsModel(this)
        viewModel?.titleObservableField?.set(title)
        viewModel?.optionsItems = optionsItems
        viewModel?.isShowConfirmObservableBoolean?.set(isShowConfirm)
        binding!!.setVariable(BR.optionsModel, viewModel)
        return binding!!.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.run {
            setCancelable(true)
            setCanceledOnTouchOutside(true)
            window
        }?.apply {
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setGravity(Gravity.BOTTOM)
            setLayout(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT
            )
        }
    }
}